//
//  DMShareCodeVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/16.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMShareCodeVC.h"
#import "DMShareCodeView.h"

@interface DMShareCodeVC ()

@property (weak, nonatomic) IBOutlet UILabel *codeLb;
@property (weak, nonatomic) IBOutlet UIImageView *codeImage;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic, strong) DMShareCodeView *shareView;

@end

@implementation DMShareCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
    
}

-(void)startInit {
    self.navigationItem.title = @"二维码";
    self.view.backgroundColor = DMDefaultColor;
    
    //生成二维码
    [self createCode];
    
    //分享
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
    [rightBtn setImage:[UIImage imageNamed:@"top_share"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(navRightClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)navRightClick {
    //弹出分享View
    if (_shareView == nil) {
        [self makeShareView];
    }
    [_shareView showView];
}
//弹出分享View
-(void)makeShareView {
    _shareView = [[DMShareCodeView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self.navigationController.view addSubview:_shareView];
    [_shareView showView];
}

- (IBAction)buttonClick:(UIButton *)sender {
    
}

#pragma mark - 生成二维码
-(void)createCode {
    //创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //过滤器恢复默认
    [filter setDefaults];
    
    //给过滤器添加数据
    NSString *string = @"https://www.baidu.com/";
    
    //将NSString格式转化成NSData格式
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    //获取二维码过滤器生成的二维码
    CIImage *image = [filter outputImage];
    
    //将获取到的二维码添加到imageview上
    _codeImage.image = [self createNonInterpolatedUIImageFormCIImage:image withSize:ScreenWidth-190];
}

/**
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

@end
